﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSTrangThaiLamViec : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public string TenTrangThai { get; set; }
        public void EditTrangThaiLamViec(int id, int nhanVienID, int loaiTrangThaiID, DateTime ngayBatDau, DateTime ngayKetThuc, string ghiChu)
        {
            List<NSTrangThaiLamViec> lst = (List<NSTrangThaiLamViec>)HttpContext.Current.Session["TrangThaiLamViec"];
            NSTrangThaiLamViec pc = lst.Where(a => a.TrangThaiID.Equals(id)).SingleOrDefault();
            pc.NhanVienID = nhanVienID;
            pc.LoaiTrangThaiID = loaiTrangThaiID;
            pc.NgayBatDau = ngayBatDau;
            pc.NgayKetThuc = ngayKetThuc;
            pc.GhiChu = ghiChu;
            pc.TenTrangThai = common.LoadCommonCodeById(loaiTrangThaiID);
         
        }
        public void DeleteTrangThaiLamViec(int id)
        {
            List<NSTrangThaiLamViec> lst = (List<NSTrangThaiLamViec>)HttpContext.Current.Session["TrangThaiLamViec"];
            NSTrangThaiLamViec pc = lst.Where(a => a.TrangThaiID.Equals(id)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllTrangThaiLamViecByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSTrangThaiLamViecs
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSTrangThaiLamViecs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }
        }
        public void UpdateTrangThaiLamViec_NhanVien(int nhanVienId, List<NSTrangThaiLamViec> nblObject)
        {
            var queryNBLByNhanVienId = (from a in db.NSTrangThaiLamViecs
                                        where a.NhanVienID.Equals(nhanVienId)
                                        select a).ToList();
            if (queryNBLByNhanVienId.Count > 0)
            {
                db.NSTrangThaiLamViecs.DeleteAllOnSubmit(queryNBLByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSTrangThaiLamViec pc in nblObject)
            {
                NSTrangThaiLamViec item = new NSTrangThaiLamViec();
                CopyNSTrangThaiLamViec(pc, item);
                db.NSTrangThaiLamViecs.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNSTrangThaiLamViec(NSTrangThaiLamViec src, NSTrangThaiLamViec des)
        {
            des.NhanVienID = src.NhanVienID;
            des.GhiChu = src.GhiChu;
            des.LoaiTrangThaiID = src.LoaiTrangThaiID;
            des.NgayKetThuc = src.NgayKetThuc;
            des.NgayBatDau = src.NgayBatDau;
        }
        public List<NSTrangThaiLamViec> LoadTrangThaiLamViecNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSTrangThaiLamViecs
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.NgayBatDau descending
                         select a).ToList();
            foreach (NSTrangThaiLamViec item in query)
            {
                item.TenTrangThai = common.LoadCommonCodeById((int)item.LoaiTrangThaiID);               
            }
            return query;

        }
        public NSTrangThaiLamViec GetTrangThaiLamViec(int nhanVienId, int loaiTrangThai, DateTime ngayBatDau, DateTime ngayKetThuc, string ghiChu)
        {
            var db = new DataClassesDataContext();
            NSTrangThaiLamViec hd = new NSTrangThaiLamViec();
            hd.TrangThaiID = 0 - DateTime.Now.Second;
            hd.TenTrangThai = common.LoadCommonCodeById(loaiTrangThai);
            hd.NhanVienID = nhanVienId;
            hd.LoaiTrangThaiID = loaiTrangThai;
            hd.NgayBatDau = ngayBatDau;
            hd.NgayKetThuc = ngayKetThuc;
            hd.GhiChu = ghiChu;          
            return hd;
        }

        public NSTrangThaiLamViec GetTrangThaiLamViecMoiNhatByNhanVienID(int nhanVienId)
        {
            var query = (from a in db.NSTrangThaiLamViecs
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.NgayBatDau descending
                         select a).ToList();
            foreach (NSTrangThaiLamViec item in query)
            {
                item.TenTrangThai = common.LoadCommonCodeById((int)item.LoaiTrangThaiID);
            }
            if (query.Count > 0)
            {
                return query[0];
            }
            else return null;
        }
    }

}